/*
** b_cd_tools.c for 42sh in /home/lingla_j/work/projet/42sh/43sh/Julien
** 
** Made by julien linglain
** Login   <lingla_j@epitech.net>
** 
** Started on  Fri Mar 30 14:40:16 2012 julien linglain
** Last update Sat Mar 31 15:17:31 2012 julien linglain
*/

#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include "mysh.h"

char            *getvalue(t_list *env, char *key)
{
  t_node        *tmp;
  char          *value;

  if (env->size != 0)
    {
      tmp = env->start;
      while (tmp != NULL)
	{
          if (my_strcmp(tmp->key, key) == 0)
            {
              value = my_strcpy(tmp->value);
              return (value);
            }
          tmp = tmp->next;
	}
    }
  return (NULL);
}

int             check_exist_access(char *s)
{
  struct stat   dir;

  if (stat(s, &dir) != 0)
    return (1);
  else
    {
      if ((access(s, R_OK)) == -1)
	{
	  my_puterror("You are not allowed to access this directory.\n");
	  return (0);
	}
      else
	return (0);
    }
}

void            cd_error(char *s)
{
  my_puterror("cd: no such file or directory: \"");
  my_puterror(s);
  my_puterror("\"\n");
}

int             my_chdir(t_list *env, char *pwd, char *path)
{
  my_add(env, "OLDPWD", pwd);
  chdir(path);
  pwd = get_current_dir_name();
  my_add(env, "PWD", pwd);
  return (EXIT_SUCCESS);
}

char		*get_var(char *var)
{
  int		i;
  int		j;
  char		*epur_var;

  i = 1;
  j = 0;
  epur_var = xmalloc(sizeof(char) * (my_strlen(var)));
  while (var[i] != '\0')
    {
      epur_var[j] = var[i];
      i++;
      j++;
    }
  epur_var[j] = '\0';
  return (epur_var);
}
